//
//  BBBlobEvent.m
//  BBTouch
//
//  Created by ben smith on 5/30/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import "BBBlobEvent.h"


@implementation BBBlobEvent

// synthesize all the simple variables

@synthesize blobID;
@synthesize deltaX;
@synthesize deltaY;
@synthesize acceleration;
@synthesize active;
@synthesize radius;
@synthesize screenLocation;
@synthesize blobLocation;


// this object is very simple for now, it could even be replaced with an NSDictionary
// but i think that it will become more complicated as the project scales up
// so I will make it a first class object now

// nice simple init
- (id)initWithLocation:(NSPoint)aPoint blobID:(int)anId;
{
	self = [super init];
	if (self != nil) {
		blobLocation = aPoint;
		blobID = anId;
		deltaX = deltaY = 0;
		radius = 0.0;
		active = YES; // they always start active
	}
	return self;
}

- (id)copyWithZone:(NSZone *)zone
{
	BBBlobEvent *copy = [[BBBlobEvent allocWithZone:zone] initWithLocation:blobLocation blobID:blobID];
	[copy setDeltaX:deltaX];
	[copy setDeltaY:deltaY];
	[copy setRadius:radius];
	[copy setAcceleration:acceleration];
	[copy setScreenLocation:screenLocation];
	[copy setBlobLocation:blobLocation];
	return copy;
}

-(void)updateScreenLocation:(NSPoint)aPoint
{
	float oldDeltaX = deltaX;
	float oldDeltaY = deltaY;

	[self setDeltaX:(aPoint.x - screenLocation.x)];
	[self setDeltaY:(aPoint.y - screenLocation.y)];
	
	[self setScreenLocation:aPoint];

	// now calculate the acceleration
	float xAccel = deltaX - oldDeltaX;
	float yAccel = deltaY - oldDeltaY;
	
	// now we want the scalar value, not the vector
	// so find the magnitude
	[self setAcceleration:hypotf(xAccel,yAccel)];
}

// a convenience method to be able to easily deactivate
// whole arrays of these things
-(void)markInactive
{
	[self setActive:NO];
}


@end
